<template>
    <div class="login_container">
        <div class="login_box">
            <!-- 头像区域 -->
            <div class="avatar_box">
                <img src="../assets/logo.png" alt="登录头像">
            </div>
               <!-- 登录表单区域 -->
            <h2 class="topic">电商管理系统</h2>
            <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="0px" class="login_form">
                <!-- 用户名 -->
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" prefix-icon="el-icon-user-solid"></el-input>
                </el-form-item>
                <!-- 密码 -->
                <el-form-item prop="password">
                    <el-input v-model="loginForm.password" prefix-icon="el-icon-lock" type="password"></el-input>
                </el-form-item>
                <!-- 按钮区域 -->
                <el-form-item class="btns">
                    <div><el-button type="primary" @click="login" class="loginBtn">登录</el-button></div>
                    <div><el-button type="info" @click="resetLoginForm" class="resetBtn">重置</el-button></div>
                </el-form-item>
            </el-form>
          </div>
    </div>
</template>

<script>
export default {
  data() {
    return {
    // 登录表单的数据绑定对象
      loginForm: {
        username: '',
        password: ''
      },
      // 表单的验证规则对象
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在3到10个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '密码长度在6到15个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 点击重置按钮，重置登录表单
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields()
    },
    login() {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return false
        const result = await this.$http.post('login', this.loginForm)
        console.log(result)
        if (result.data.meta.status !== 200) return this.$message.error('登录失败!')
        this.$message.success('登录成功！')
        // 1、将登录成功后的token，保存到客户端的 sessionStorage 中
        //  1.1 项目中除了登录的其他API接口，必须在登录后才能访问
        //  1.2 token 只在当前网站打开期间有效，所以将 token 保存在 sessionStoarg 中
        window.sessionStorage.setItem('token', result.data.data.token)
        // 2、通过编程式导航跳转到后台主页，路由地址是 /home
        this.$router.push('/home')
      })
    }
  }
}

</script>

// scoped表示只在当前组件生效
<style lang="less" scoped>
    .login_container {
        background: url('../assets/background.jpg');
        height: 100%;
    }

    .login_box {
        width: 350px;
        height: 400px;
        background-color: #fff;
        border-radius: 3px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        box-shadow: 0px 0px 5px black;
        background-color: rgba(0, 0, 0, .5);
    }

    .avatar_box {
        height: 130px;
        width: 130px;
        background: rgba(255, 255, 255, .5);
        border: 1px solid #eee;
        border-radius: 50%;
        box-shadow: 0 0 10px #ddd;
        padding: 10px;
        position: absolute;
        left: 50%;
        transform: translate(-50%, -50%);
        img {
            width: 100%;
            height: 100%;
            border-radius: 50%;
            background-color: #eee;
        }
    }

    .topic {
      position: absolute;
      top: 65px;
      left: 50%;
      transform: translate(-50%, 0);
      color: #fff;
      text-shadow: 0px 0px 2px #fff;
    }

    .btns {
      display: flex;
      justify-content: center;
    }

    .btns .loginBtn {
      width: 260px;
      margin-top: 20px;
      margin-bottom: 10px
    }

    .btns .resetBtn {
      width: 260px;
    }

    .login_form {
      position: absolute;
      top: 130px;
      width: 100%;
      padding: 0 20px;
      box-sizing: border-box;
    }
</style>
